package com.fast.common.exception;

import java.text.MessageFormat;

import org.apache.commons.lang3.StringUtils;

import com.fast.common.util.ErrorMsg;

public class FastException extends Exception {
	/**
	 */
	private static final long serialVersionUID = -3429944411495272754L;

	private String message;
	private String errorCode;

	public FastException(String message, Throwable cause) {
		super(message, cause);
	}

	public FastException(String errorCode ) {
		super(errorCode);
		this.errorCode=errorCode;
		this.message = ErrorMsg.getMsg(errorCode) ;
	}
	public FastException(String errorCode , Object... arguments ) {
		super(errorCode);
		this.errorCode = errorCode;
		this.message = MessageFormat.format( ErrorMsg.getMsg(errorCode) , arguments) ;
	}
	public FastException(String errorCode, String message) {
		super(errorCode);
		this.errorCode = errorCode;
		String msg = ErrorMsg.getMsg(errorCode);
		if( StringUtils.isNotBlank(msg)){
			this.message = MessageFormat.format( msg , message) ;
		}else{
			this.message = message ;
		}
	}

	@Override
	public String getMessage() {
		String msg = StringUtils.trimToNull(message);
		if (StringUtils.isEmpty(msg)) {
			return super.getMessage();
		}
		return message;
	}

	public String getErrorCode() {
		return errorCode;
	}

}
